package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.Activity.InfoDetals;
import liumengbaio.lolzhangyoubao.Activity.InFo.ZhangYouBao;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/20.
 */
public class XrecycleviewAdapter extends RecyclerView.Adapter<XrecycleviewAdapter.ViewHolder> {
    private Context mContext;
    private List<ZhangYouBao.DataBean> list;
    private String pamas="";
    private String path_next;

    public XrecycleviewAdapter(Context mContext, List<ZhangYouBao.DataBean> list) {
        this.mContext = mContext;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_lol, null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                path_next = "http://lol.zhangyoubao.com/mobiles/item/" +pamas+
                        "?user_id=&token=&i_=EAC1B788-00BC-454A-A9B" +
                        "9-460852CFC011&t_=1438745542&p_=18971&v_" +
                        "=40050303&d_=ios&osv_=8.3&version=0&a_=lol&size=middle";
                Log.i("001", path_next);
                Intent intent = new Intent(mContext, InfoDetals.class);
                Bundle bundle = new Bundle();
                bundle.putString("url", path_next);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Picasso.with(mContext).load(list.get(position).getPic_url()).into(holder.iv_lol);
        holder.tv_comment.setText(list.get(position).getComment());
        holder.tv_title.setText(list.get(position).getTitle());
        int date_published = list.get(position).getPublished();
        pamas=list.get(position).getId();
        long date = date_published * 1000L;
        Date d = new Date(date);
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
        String dateStr = format.format(d);
        holder.tv_date.setText(dateStr);
        if (list.get(position).getRecommend() == 1) {
            holder.tv_comment.setVisibility(View.INVISIBLE);
            holder.iv_hot.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_lol, iv_hot;
        private TextView tv_title, tv_date, tv_comment;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_lol = (ImageView) itemView.findViewById(R.id.iv_lol);
            iv_hot = (ImageView) itemView.findViewById(R.id.iv_hot);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_date = (TextView) itemView.findViewById(R.id.tv_date);
            tv_comment = (TextView) itemView.findViewById(R.id.tv_comment);
        }
    }
}
